<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>发送邮件附件</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="Han2PinYin.html">将汉字转为拼音的模块</a>&nbsp;&nbsp;>>Next: <a href="050328.html">Catalyst & Eplanet</a></p>
<h1>发送邮件附件</h1>
<div class='content'>
<p>Category: <a href='Modules.html'>Modules</a> &nbsp; Keywords: <b>attachment email MIME::Lite auth Net::SMTP</b></p><h2>简单描述</h2>
如果仅仅只是发送不带附件的邮件，请参考<a href='NetSMTP.html'>如何用Net::SMTP发送邮件</a>。<p>

这里使用 MIME::Lite 模块来发送附件。分为两种情况，一种是 SMTP 服务器不需要验证的，另一种是需要验证/auth 的。

<h2>情况一：SMTP 服务器不需要验证</h2>
这种情况下可以参考 perldoc <a href='http://search.cpan.org/perldoc?MIME::Lite'>MIME::Lite</a>. 还有个更详细的例子在 <a href='http://www.perl.com/pub/a/2003/09/03/perlcookbook.html?page=2'>Cooking with perl</a>.<br>
一个简单的 Example:
<pre><code>
use MIME::Lite;
 
my $msg = MIME::Lite->new(From    => 'sender@example.com',
             To      => 'recipient@example.com',
             Subject => 'My photo for the brochure',
             Type    => 'multipart/mixed');
$msg->attach(Type        => 'image/jpeg',
             Path        => '/Users/gnat/Photoshopped/nat.jpg',
             Filename    => 'gnat-face.jpg');
$msg->attach(Type        => 'TEXT',
             Data        => 'I hope you can use this!');
#$msg->send(  );            # default is to use sendmail(1)
# now we use smtp.
$msg->send('smtp', 'mailserver.example.com');
</code></pre>

<h2>情况二：SMTP 服务器需要验证/auth</h2>
现在的 SMTP 服务器大部分都需要验证，我猜测可能的原因是为了防止垃圾邮件。<br>
MIME::Lite 中是不支持 SMTP 验证/auth 的。所以我们一般将 MIME::Lite 转为 string, 然后通过验证后的 Net::SMTP 来发送。详细的代码如下（已测试成功）：
<pre><code>
use Net::SMTP;
use MIME::Lite;

# setting
my $mailhost = 'smtp.163.com';
my $username = 'usr';
my $password = 'pwd';
my $from = 'usr@163.com'; # your email on that smtp host
my $to = 'recipient@example.com'; # the recipient
my $subject = 'It\'s a test mail with attachment'; # the email title
my $content = 'emailed from fayland.'; # the content of email
my $attachment = 'E:/test.gif';

$smtp = Net::SMTP->new($mailhost, Timeout => 120, Debug => 1);

# anth login
$smtp->auth($username, $password);

# attachment
my $msg = MIME::Lite->new(
    From    => $from,
    To      => $to,
    Subject => $subject,
    Type    => 'TEXT',
    Data    => $content,
);

# Attach 
$msg->attach(
    Type     => 'image/gif', # the attachment mime type
    Path     => $attachment, # local address of the attachment
    Filename => 'asuwish.gif', # the name of attachment in email
);

my $str = $msg->as_string() or die "Convert the message as a string: $!\n";

$smtp->mail($from);
$smtp->to($to);
$smtp->data();
$smtp->datasend($str);
$smtp->dataend();
$smtp->quit;
</code></pre>

<h2>参考</h2>
<ul>
<li><a href='http://forums.devshed.com/archive/t-184255/Send-Attachments-using-MimeLite-and-NetSMTP'>http://forums.devshed.com/archive/t-184255/Send-Attachments-using-MimeLite-and-NetSMTP</a><br>
Send Attachments using Mime::Lite and Net::SMTP?
<li>perldoc <a href='http://search.cpan.org/perldoc?MIME::Lite'>MIME::Lite</a>, <a href='http://search.cpan.org/perldoc?Net::SMTP'>Net::SMTP</a>
</ul></div>
<p><&lt;Previous: <a href="Han2PinYin.html">将汉字转为拼音的模块</a>&nbsp;&nbsp;>>Next: <a href="050328.html">Catalyst & Eplanet</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E9%99%84%E4%BB%B6&url=http://www.fayland.org/journal/EmailWithAttachment.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='Catalyst_Authenticate.html'>Catalyst Authenticate</a> < <span class='digit'>2005-09-26 12:50:39</span> ></li><li><a href='modperl_PerlAuthenHandler.html'>modperl 的用户验证</a> < <span class='digit'>2005-11-26 00:08:37</span> ></li></ul>
Created on <span class="digit">2005-03-23 22:18:40</span>, Last modified on <span class="digit">2005-05-21 00:14:25</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>